<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use App\Models\Base\LogBanned;
use App\Models\Base\ServerList;
use App\Service\RequestService;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Illuminate\Support\Facades\Input;

/**
 * 角色禁言日志.
 */
class RoleBannedLogController extends Controller
{
    use ModelForm;

    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header(trans('game.player_related'));
            $content->description(trans('game.role_banned_log'));
            $content->body($this->grid()->render());
        });
    }

    protected function grid()
    {
        $grid = Admin::grid(LogBanned::class, function (Grid $grid) {
            $grid->model()->orderBy('id', 'desc');
            $grid->id('ID');
            $grid->serverName()->name(trans('game.server_name'));
            $grid->columns([
                'player_id' => trans('game.role_id'),
                'player_name' => trans('game.role_name'),
            ]);
            $grid->status(trans('game.status'))->display(function ($status) {
                return $status ? "<span class='label bg-green'>".trans('game.normal')."</span>" : "<span class='label bg-red'>".trans('game.lock')."</span>";
            });
            $grid->columns([
                'created_at' => trans('admin.created_at'),
                'updated_at' => trans('admin.updated_at'),
            ]);
            $grid->disableRowSelector();
            $grid->disableCreateButton();
            $grid->actions(function ($actions) use ($grid) {
                $actions->disableDelete();
            });
        });

        return $grid;
    }

    protected function form()
    {
        return Admin::form(LogBanned::class, function (Form $form) {
            $form->radio('status', trans('game.status'))->options(['1' => trans('game.normal')])->rules('required');
            $form->saving(function (Form $form) {
                $status = Input::post('status');
                if (null == $status) {
                    return back()->with(admin_toastr(trans('game.select_state'), 'error'))->withInput();
                }
                if ('PUT' == request()->method()) {
                    //解除禁言
                    $ret = RequestService::bannedRole($form->model()->server_id, $form->model()->player_name, 0, 0);
                    if (!$ret) {
                        return back()->with(admin_toastr(trans('game.gs_communication_failed'), 'error'))->withInput();
                    }
                }
            });
        });
    }

    protected function addForm()
    {
        return Admin::form(LogBanned::class, function (Form $form) {
            $form->select('server_id', trans('game.server_name'))->options(ServerList::all()->pluck('name', 'server_id'))->rules('required')->load('name', route('api.player'));
            $form->select('name', trans('game.role_name'));
        });
    }

    /**
     * [create 添加禁言].
     *
     * @Author   王文凡
     * @DateTime 2018-04-11
     *
     * @version  1.0
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {
            $content->header(trans('game.player_related'));
            $content->description(trans('game.add_banned'));
            $content->body($this->addForm());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     *
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $content->header(trans('game.player_related'));
            $content->description(trans('game.edit_banned'));
            $content->body($this->form()->edit($id));
        });
    }
}
